package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.UserRole;
import com.example.result.Result;
import com.example.service.UserRoleService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 用户角色控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class UserRoleController {


    @Autowired
    UserRoleService userRoleService;

    // ===============================用户角色管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加用户角色信息
     */
    @RequestMapping(value = "/userRoles", method = RequestMethod.POST)
    public Result addUserRole(@RequestBody String json) {
        System.out.println(json);

//		UserRole userRole = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), UserRole.class);

        return userRoleService.addUserRole(json);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新用户角色信息
     */
    @RequestMapping(value = "/userRoles", method = RequestMethod.PUT)
    public Result updateUserRole(@RequestBody String json) {
        // 将JSON数据转换为UserRole对象实体
        JSONObject parseObject = JSON.parseObject(json);
        UserRole userRole = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), UserRole.class);
        return userRoleService.updateUserRole(userRole);

    }

    /**
     * @param userRoleId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据用户角色编号删除用户角色信息
     */
    @RequestMapping(value = "/userRoles/{userRoleId}", method = RequestMethod.DELETE)
    public Result deleteUserRoleById(@PathVariable("userRoleId") String userRoleId) {
        return userRoleService.deleteUserRoleById(userRoleId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有用户角色信息
     */
    @RequestMapping(value = "/userRoles", method = RequestMethod.GET)
    public Result queryAllUserRole() {
        return userRoleService.queryAllUserRole();

    }

    /**
     * @param userRoleId
     * @return 以JSON格式返回该用户角色信息
     * @describe 根据编号查询用户角色信息
     */
    @RequestMapping(value = "/userRoles/{userRoleId}", method = RequestMethod.GET)
    public Result queryUserRoleById(@PathVariable("userRoleId") String userRoleId) {
        return userRoleService.queryUserRoleById(userRoleId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加用户角色信息
     */
    @RequestMapping(value = "/userRoles/excels", method = RequestMethod.POST)
    public Result addUserRoleByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<UserRole> list = JSON.parseArray(parseObject.getString("json"), UserRole.class);

        System.out.println(list);

        return userRoleService.addUserRoleByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据用户角色编号批量删除用户角色信息
     */
    @RequestMapping(value = "/userRoles/dels", method = RequestMethod.POST)
    public Result deleteUserRoleByIds(@RequestBody Map<String, String> Ids) {

        return userRoleService.deleteUserRoleByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 用户角色信息模糊查询
     */
    @RequestMapping(value = "/userRoles/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return userRoleService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有没有分配角色的用户
     */
    @RequestMapping(value = "/userRoles/userNotRoles", method = RequestMethod.GET)
    public Result queryAllUserOfNotRole() {
        return userRoleService.queryAllUserOfNotRole();

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有角色名称
     */
    @RequestMapping(value = "/userRoles/roleNames", method = RequestMethod.GET)
    public Result queryAllRoleName() {
        return userRoleService.queryAllRoleName();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有用户和角色的已分配信息
     */
    @RequestMapping(value = "/userRoles/userAndRoles", method = RequestMethod.GET)
    public Result queryAllUserAndRoleInfo() {
        return userRoleService.queryAllUserAndRoleInfo();
    }
}
